<?php
    /*

     Version: MPL 1.1

     Software distributed under the License is distributed on an "AS IS"
     basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
     License for the specific language governing rights and limitations
     under the License.

     The Original Code is KASSIOP Framework.

     The Initial Developer of the Original Code is SURIAN Nicolas (aka NairuS) <me@nairus.fr>.
     Portions created by the Initial Developer are Copyright (C) 2010
     The Initial Developer. All Rights Reserved.

     Contributor(s):

     Alternatively, the contents of this file may be used under the terms
     of the MPL license Version 1.1 (the  "MPL" License"), in which case the
     provisions of Version: MPL 1.1 License are applicable instead of those
     above.  If you wish to allow use of your version of this file only
     under the terms of the MPL License and not to allow others to use
     your version of this file under the MPL, indicate your decision by
     deleting  the provisions above and replace  them with the notice and
     other provisions required by the MPL License.  If you do not delete
     the provisions above, a recipient may use your version of this file
     under either the MPL License.

     The contents of this file are subject to the Mozilla Public License
     Version 1.1 (the "License"); you may not use this file except in
     compliance with the License. You may obtain a copy of the License at
     http://www.mozilla.org/MPL/

    */


    require_once 'PHPUnit/Framework.php';
    require_once dirname( __FILE__ ) . '/../../../../kassiop/core/Autoload.php' ;
    Autoload::register() ;

    /**
     * The unit test class of the HtmlHelper class.
     *
     * @package unittests.kassiop.system
     * @subpackage helpers
     * @author nairus
     *
     */
    class HtmlHelperTest extends PHPUnit_Framework_TestCase
    {
        /**
         * Defines the html string of the excepted date tags.
         *
         * @var string
         */
        protected $exceptedDate;

        /**
         * Defines the html string of the excepted datetime tags.
         *
         * @var string
         */
        protected $exceptedDateTime;

        /**
         * Defines the array of custom monthes to pass.
         *
         * @var array
         */
        protected $monthes;

        /**
         * String representation of seconds tag.
         *
         * @var string
         */
        protected $seconds ;

        /**
         * Defines the array of custom years to pass.
         *
         * @var array
         */
        protected $years;

        protected function setUp()
        {
            $this->exceptedDate      = '<select id="date_day" name="date[day]"><option value=""></option><option value="01">01</option><option value="02">02</option><option value="03">03</option><option value="04">04</option><option value="05">05</option><option value="06">06</option><option value="07">07</option><option value="08">08</option><option value="09">09</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option><option value="21">21</option><option value="22">22</option><option value="23">23</option><option value="24">24</option><option value="25">25</option><option value="26">26</option><option value="27">27</option><option value="28">28</option><option value="29">29</option><option value="30">30</option><option value="31">31</option></select><select id="date_month" name="date[month]"><option value=""></option><option value="01">January</option><option value="02">February</option><option value="03">March</option><option value="04">April</option><option value="05">May</option><option value="06">June</option><option value="07">July</option><option value="08">August</option><option value="09">September</option><option value="10">October</option><option value="11">November</option><option value="12">December</option></select><select id="date_year" name="date[year]"><option value=""></option><option value="2010">2010</option><option value="2011">2011</option><option value="2012">2012</option></select>';
            $communElement           = '<option value=""></option><option value="00">00</option><option value="01">01</option><option value="02">02</option><option value="03">03</option><option value="04">04</option><option value="05">05</option><option value="06">06</option><option value="07">07</option><option value="08">08</option><option value="09">09</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option><option value="21">21</option><option value="22">22</option><option value="23">23</option>';
            $communElement2          = $communElement . '<option value="24">24</option><option value="25">25</option><option value="26">26</option><option value="27">27</option><option value="28">28</option><option value="29">29</option><option value="30">30</option><option value="31">31</option><option value="32">32</option><option value="33">33</option><option value="34">34</option><option value="35">35</option><option value="36">36</option><option value="37">37</option><option value="38">38</option><option value="39">39</option><option value="40">40</option><option value="41">41</option><option value="42">42</option><option value="43">43</option><option value="44">44</option><option value="45">45</option><option value="46">46</option><option value="47">47</option><option value="48">48</option><option value="49">49</option><option value="50">50</option><option value="51">51</option><option value="52">52</option><option value="53">53</option><option value="54">54</option><option value="55">55</option><option value="56">56</option><option value="57">57</option><option value="58">58</option><option value="59">59</option>';
            $this->exceptedDateTime  = $this->exceptedDate;
            $this->exceptedDateTime .= '<select id="date_hour" name="date[hour]">' . $communElement . '</select><select id="date_minute" name="date[minute]">' . $communElement2 . '</select>';
            $this->seconds           = '<select id="date_second" name="date[second]">' . $communElement2 . '</select>';
            $this->monthes           = array( '01' => 'January', '02' => 'February', '03' => 'March', '04' => 'April', '05' => 'May', '06' => 'June', '07' => 'July', '08' => 'August', '09' => 'September', '10' => 'October', '11' => 'November', '12' => 'December' ) ;
            $this->years             = array( '2010' => '2010', '2011' => '2011', '2012' => '2012' );
        }

        protected function tearDown()
        {
            unset( $this->exceptedDate, $this->exceptedDateTime, $this->monthes, $this->seconds, $this->years ) ;
        }

        public function testButton()
        {
            $this->assertEquals( HtmlHelper::button(), '<input type="button" />', 'The input tag must be typed button.' );
            $this->assertEquals( HtmlHelper::button( array( 'name' => 'myButton', 'value' => 'Envoyer' ) ), '<input name="myButton" value="Envoyer" type="button" />', 'The tag "input" must have attributes "name" and "value"' );
        }

        public function testCheckbox()
        {
            $this->assertEquals( HtmlHelper::checkbox( 'myCheckbox' ), '<input name="myCheckbox" type="checkbox" />', 'The tag "input" must have attributes "name" and typed "checkbox"' );
            $this->assertEquals( HtmlHelper::checkbox( 'myCheckbox', array( 'id' => 'myCheckbox' ) ), '<input id="myCheckbox" name="myCheckbox" type="checkbox" />', 'The tag "input" must have attributes "name" and "id"' );
            $this->assertEquals( HtmlHelper::checkbox( 'myCheckbox', array( 'id' => 'myCheckbox' ), true ), '<input id="myCheckbox" name="myCheckbox" type="checkbox" checked="checked" />', 'The checkbox must be checked' );
        }

        public function testDate()
        {
            $this->assertEquals( HtmlHelper::date( 'date', $this->years, $this->monthes, true ), $this->exceptedDate, 'The date tags must be well written.' );
        }

        public function testDateTime()
        {
            $this->assertEquals( HtmlHelper::dateTime( 'date', $this->years, $this->monthes, true ), $this->exceptedDateTime, '1.1 - The datetime tags must be well written without seconds.' );
            $this->assertEquals( HtmlHelper::dateTime( 'date', $this->years, $this->monthes, true, false, true ), $this->exceptedDateTime . $this->seconds, 'The datetime tags must be well written with seconds.' );
        }

        public function testErrorForm()
        {
            $this->assertEquals( HtmlHelper::errorForm( 'My test message' ), '<span class="error">My test message</span>', 'The span tag must be well built' );
        }

        public function testFile()
        {
            $this->assertEquals( HtmlHelper::file( 'myFile' ), '<input name="myFile" type="file" />', 'The tag "input" must have attribute "name" and  be typed file' );
        }

        public function testHidden()
        {
            $this->assertEquals( HtmlHelper::hidden( 'myHidden', 'hidden' ), '<input name="myHidden" value="hidden" type="hidden" />', 'The input tag must be typed hidden and have attributes "name" and "value".' );
        }

        public function testImage()
        {
            $this->assertEquals( HtmlHelper::image( '/path/to/my/image.png' ), '<img src="/path/to/my/image.png" alt="" />', 'The <img> html tag must be xhtml valid.' ) ;
            $this->assertEquals( HtmlHelper::image( '/path/to/my/image.png', 'My image', array( 'class' => 'myclass' ) ), '<img src="/path/to/my/image.png" alt="My image" class="myclass" />', 'The class property of the <img> html tag must be xhtml valid.' ) ;
        }

        public function testInputImage()
        {
            $this->assertEquals( HtmlHelper::inputImage( '/path/to/my/image.png' ), '<input alt="" src="/path/to/my/image.png" type="image" />', 'The input tag must have "src" attribute and be typed image.' );
            $this->assertEquals( HtmlHelper::inputImage( '/path/to/my/image.png', 'image.png', array( 'name' => 'myButtonImage' ) ), '<input name="myButtonImage" alt="image.png" src="/path/to/my/image.png" type="image" />', 'The tag "input" must have attributes "src", "name" and "alt"' );
        }

        public function testInputText()
        {
            $this->assertEquals( HtmlHelper::inputText( 'myInputText' ), '<input name="myInputText" type="text" />', 'The tag "input" must have attributes "name" and typed "text"' );
            $this->assertEquals( HtmlHelper::inputText( 'myInputText', array( 'id' => 'myInputText' ) ), '<input id="myInputText" name="myInputText" type="text" />', 'The tag "input" must have attributes "name" and "id"' );
        }

        public function testLinkTo()
        {
            $this->assertEquals( HtmlHelper::linkTo( 'My link', '/url/to/link' ), '<a href="/url/to/link">My link</a>', 'The <a> html tag must be xhtml valid.' ) ;
            $this->assertEquals( HtmlHelper::linkTo( 'My link', '/url/to/link', array( 'class' => 'myclass' ) ), '<a href="/url/to/link" class="myclass">My link</a>', 'The class property of the <a> html tag must be xhtml valid.' ) ;
            $this->assertEquals( HtmlHelper::linkTo( 'My link', '/url/to/link', array( 'class' => 'myclass' ), 'Are you sure ?' ), '<a href="/url/to/link" class="myclass" onclick="return confirm(\'Are you sure ?\');">My link</a>', 'The class property of the <a> html tag must be xhtml valid.' ) ;
        }

        public function testParagraphe()
        {
            $this->assertEquals( HtmlHelper::paragraphe( 'My paragraphe' ), '<p>My paragraphe</p>', 'The <p> html tag must be xhtml valid.' ) ;
            $this->assertEquals( HtmlHelper::paragraphe( 'My paragraphe', array( 'class' => 'myclass' ) ), '<p class="myclass">My paragraphe</p>', 'The class property of the <p> html tag must be xhtml valid.' ) ;
        }

        public function testPassword()
        {
            $this->assertEquals( HtmlHelper::password( 'myPassword' ), '<input name="myPassword" type="password" />', 'The tag "input" must have attributes "name" and typed "password"' );
            $this->assertEquals( HtmlHelper::password( 'myPassword', array( 'id' => 'myPassword' ) ), '<input id="myPassword" name="myPassword" type="password" />', 'The tag "input" must have attributes "name" and "id"' );
        }

        public function testRadio()
        {
            $this->assertEquals( HtmlHelper::radio( 'myRadio', 'value' ), '<input name="myRadio[]" value="value" type="radio" />', 'The tag "input" must have attributes "name" and "value" and typed "radio"' );
            $this->assertEquals( HtmlHelper::radio( 'myRadio', 'value', array( 'id' => 'myRadio_1' ) ), '<input id="myRadio_1" name="myRadio[]" value="value" type="radio" />', 'The tag "input" must have attributes "name" and "id"' );
            $this->assertEquals( HtmlHelper::radio( 'myRadio', 'value', array( 'id' => 'myRadio_1' ), true ), '<input id="myRadio_1" name="myRadio[]" value="value" type="radio" checked="checked" />', 'The radio must be checked' );
        }

        public function testReset()
        {
            $this->assertEquals( HtmlHelper::reset(), '<input type="reset" value="Cancel" />', 'The input tag must be typed reset.' );
            $this->assertEquals( HtmlHelper::reset( array( 'name' => 'myResetButton', 'value' => 'Annuler' ) ), '<input name="myResetButton" value="Annuler" type="reset" />', 'The tag "input" must have attributes "name" and "value"' );
        }

        public function testSelect()
        {
            $datas = array( '1' => 'First entry', '2' => 'Second entry', '3' => 'Third entry' );
            $this->assertEquals( HtmlHelper::select( 'mySelect' ), '<select name="mySelect"></select>', 'The select tag mus have "name" attribute.' ) ;
            $this->assertEquals( HtmlHelper::select( 'mySelect', $datas ), '<select name="mySelect"><option value="1">First entry</option><option value="2">Second entry</option><option value="3">Third entry</option></select>', 'The select tag must have the 3 "option" tags.' ) ;
            $this->assertEquals( HtmlHelper::select( 'mySelect', $datas, array( 'id' => 'mySelect') ), '<select id="mySelect" name="mySelect"><option value="1">First entry</option><option value="2">Second entry</option><option value="3">Third entry</option></select>', 'The select tag must have an "id" attribute .' ) ;
            $this->assertEquals( HtmlHelper::select( 'mySelect', $datas, array( 'id' => 'mySelect'), true ), '<select id="mySelect" name="mySelect"><option value=""></option><option value="1">First entry</option><option value="2">Second entry</option><option value="3">Third entry</option></select>', 'The select tag must have an empty "option" tags.' ) ;
            $this->assertEquals( HtmlHelper::select( 'mySelect', $datas, array( 'id' => 'mySelect'), true, 2 ), '<select id="mySelect" name="mySelect"><option value=""></option><option value="1">First entry</option><option value="2" selected="selected">Second entry</option><option value="3">Third entry</option></select>', 'The second option must be selected.' ) ;
        }

        public function testSubmit()
        {
            $this->assertEquals( HtmlHelper::submit(), '<input value="Submit" type="submit" />', 'The input tag must be typed submit.' );
            $this->assertEquals( HtmlHelper::submit( array( 'name' => 'mySubmitButton', 'value' => 'Envoyer' ) ), '<input name="mySubmitButton" value="Envoyer" type="submit" />', 'The tag "input" must have attributes "name" and "value"' );
        }

        public function testTextArea()
        {
            $this->assertEquals( HtmlHelper::textArea('myTextArea', 'MyTextArea' ), '<textarea name="myTextArea" cols="50" rows="5">MyTextArea</textarea>', 'The textarea tag must be right written.' ) ;
            $this->assertEquals( HtmlHelper::textArea('myTextArea', 'MyTextArea', 20, 10, array( 'id' => 'myTextArea') ), '<textarea id="myTextArea" name="myTextArea" cols="20" rows="10">MyTextArea</textarea>', 'The textarea tag must be right written.' ) ;
        }
    }
?>